package com.ropeok.controller;



import com.ropeok.Result.Result;
import com.ropeok.entity.RoleAndPermission;
import com.ropeok.mapper.role_permissionMapper;

import com.ropeok.service.serviceimpl.ServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags = "操作角色-权限关系表")
@RestController
public class role_permissionController {
    @Autowired
    role_permissionMapper role_permissionMapper;

    @Autowired
    ServiceImpl service;
    //为某个角色增加某个权限
    @ApiOperation(value = "增加信息",notes = "添加权限")
    @PostMapping(value = "/addRoleAndPermission")
    public Result save(@Valid RoleAndPermission RaP,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        role_permissionMapper.SaveRp(RaP);
        return Result.success();
    }

    @ApiOperation(value = "删除中间表信息",notes = "通过角色id删除中间表对应信息")
    @GetMapping(value = "/del{rid}")
    public Result deleter(@PathVariable("rid") Integer rid){
        Result result;
        if(null==rid){
            result=Result.error("id不能为空");
        }else {
            service.DelRoleAndPermissionByRole(rid);
            result=Result.success();
        }
        return result;
    }

    @ApiOperation(value = "删除中间表信息",notes = "通过权限id删除对应信息")
    @GetMapping(value = "/del{pid}")
    public Result deletep(Integer pid){
        Result result;
        if(null==pid){
            result=Result.error("id不能为空");
        }else {
            service.DelRoleAndPermissionByPermission(pid);
            result=Result.success();
        }
        return result;
    }

}
